package de.tum.in.eist.poll.server;

import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.tum.in.eist.poll.pub.service.PollService;
import de.tum.in.eist.poll.pub.shared.Feedback;
import de.tum.in.eist.poll.pub.shared.Lecture;
import de.tum.in.eist.poll.pub.shared.User;

public class Server1 extends RemoteServiceServlet implements PollService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public User login(String name, String password)
			throws IllegalArgumentException {
		// check if the user is registered:
		User u = Database1.getUser(name);
		if (u == null) {
			return null;
		}
		// check if the user entered the correct password:
		if (u.getPassword().equals(password)) {
			// return the User:
			return u;
		} else {
			return null;
		}
	}

	@Override
	public List<Lecture> requestLectures(User user) {
		// returns a list containing all lectures
		return Database1.getLectures();
	}

	@Override
	public List<Feedback> requestFeedback(Lecture lecture) {
		// returns a list containing all feedbacks for the given lectures
		return Database1.getFeedbackForLecture(lecture);
	}

	@Override
	public String submitFeedback(Feedback feedback) {
		// sends the given feedback to the database
		// check if the transmission was successful and return a message:
		if (!feedback.getLecture().containsStudent(feedback.getStudent())) {
			Database1.collectFeedback(feedback);
			return "The transmission was successful. Thank you very much for your feedback.";
		} else {
			return "Unfortunately it is not possible to upload your feedback. If you did not upload a feedback yet, please contact an administrator.";
		}
	}

}
